<?php


App::uses('Crud', 'Model');

class AnsweredQuestion extends Crud
{  
    /**
     * Regresa un reseumen de las preguntas contestadas y las correctas
     */
    public function getResume($examId) {
        $dataCount = array(
            'conditions' => array(
                'AppQuestions.exam_id' => $examId
            ),
            'joins' => array(
                array(
                    'table' => 'applied_questions',
                    'alias' => 'AppQuestions',
                    'conditions' => array(
                        'AnsweredQuestion.applied_question_id = AppQuestions.id',
                    )
                )
            ),
            'group' => array(
                'applied_question_id'
            ),
        );
       
        return array(
            'total' => $this->find('count',$dataCount),
            'right' => 0
        );
    }
    
    public function getAnsweredQuestionIds( $examId )
    {
        $dataCount = array(
            'fields' => array(
                'applied_question_id as id_a_q'
            ),
            'conditions' => array(
                'AppQuestions.exam_id' => $examId
            ),
            'joins' => array(
                array(
                    'table' => 'applied_questions',
                    'alias' => 'AppQuestions',
                    'conditions' => array(
                        'AnsweredQuestion.applied_question_id = AppQuestions.id',
                    )
                )
            ),
            'group' => array(
                'applied_question_id'
            )
        );
        $records = $this->find('all',$dataCount);
        $answeredQuestions = $this->flatArray($records);
        
        $ids = array();
        foreach( $answeredQuestions as $key => $aq )
        {
            array_push($ids, $aq['id_a_q']);
        }
        return $ids;        
    }
    
    public function getIdAnswersBy( $questionId )
    {
        $dataCount = array(
            'fields' => array(
                'applied_answer_id as id_a_a'
            ),
            'conditions' => array(
                'applied_question_id' => $questionId
            )
        );
        $records = $this->find('all',$dataCount);
        $answeredQuestions = $this->flatArray($records);
        
        $ids = array();
        foreach( $answeredQuestions as $key => $aq )
        {
            array_push($ids, $aq['id_a_a']);
        }
        return $ids; 
    }
}